//
//  HMSinaAddButtonClosureView.swift
//  ZJCSina
//
//  Created by 朱建超 on 16/11/23.
//  Copyright © 2016年 itHeiMa. All rights reserved.
//

import UIKit
import pop

class HMSinaAddButtonClosureView: UIView {

    lazy var buttonArray : [HMSinaComposeButton] = [HMSinaComposeButton]()
    
//    //展现撰写的视图
//    func show(fromViewController : UIViewController?) {
//        
//        //传递控制器
//        fromViewController?.view.addSubview(self)
//        
//        //执行动画
//        for (index , button) in buttonArray.enumerated() {
//            
//            self.startAnimation(button: button, index: index, isShow: true)
//            
//        }
//    }
    
    override init(frame: CGRect) {
        
        super.init(frame: UIScreen.main.bounds)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //按钮消失
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        //实例化弹性动画的对象
        //添加动画对象(enumerated能取到下标和对象)
        for (index,button) in buttonArray.reversed().enumerated() {
            
            self.startAnimation(button: button, index: index, isShow: false)
            
        }
        
        //动画执行完毕之后，移除这个视图
        //延迟移除
        
        
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.5) {
            self.removeFromSuperview()
        }
        
    }
    
    //按钮出现
    override func willMove(toSuperview newSuperview: UIView?) {
        
        super.willMove(toSuperview: newSuperview)
        
        //执行动画
        for (index , button) in buttonArray.enumerated() {
            
            self.startAnimation(button: button, index: index, isShow: true)
            
        }
        
    }
    
    //开启按钮的动画效果
    private func startAnimation(button : HMSinaComposeButton , index : Int , isShow : Bool) {
        
        //实例化弹性动画的对象
        //添加动画对象(enumerated能取到下标和对象)
            let animation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
            
            //设置属性
            animation?.springBounciness = 10
            animation?.springSpeed = 10
            //设置延迟时间
            animation?.beginTime = CACurrentMediaTime() + Double(index) * 0.05
            
            var buttonY : CGFloat
            
            if isShow == true {
                
                buttonY = button.center.y - 350
                
            }else{
            
                buttonY = button.center.y + 350
            
            }
            
            animation?.toValue = CGPoint(x: button.center.x, y: buttonY)
            
            button.pop_add(animation, forKey: nil)
        
    }
    
    private func setupUI() {
        
//        //添加被处理毛玻璃效果的背景图  截取屏幕
//        
//        //截屏
//        let window = UIApplication.shared.keyWindow
//        //开启图片的上下文
//        UIGraphicsBeginImageContextWithOptions((window?.frame.size)!, false, 0)
//        //将当前的window绘制到图形的上下文
//        window?.drawHierarchy(in: (window?.frame)!, afterScreenUpdates: true)
//        //从图片的上下文中获取图片
//        let image = UIGraphicsGetImageFromCurrentImageContext()
//        //关闭图片的上下文
//        UIGraphicsEndImageContext()
        
//        let backView = UIImageView(image: UIImage.jieQuScreen().applyDarkEffect())
        
        self.addSubview(backView)
        self.addSubview(backGroundView)
        
        backGroundView.snp.makeConstraints { (make) in
            
            make.centerX.equalTo(self.snp.centerX)
            make.top.equalTo(self.snp.top).offset(180)
            
        }
        
        addButtons()
    }
    
    //添加按钮
    private func addButtons() {
        
        let buttonJianJu = (UIScreen.main.bounds.width - 3 * buttonImageWidth) / 4
        
        for i in 0..<6 {
            
            //应该确定循环中对应的列的索引
            let hangIndex = i / 3
            let lieIndex = i % 3
            
            let buttonX = buttonJianJu + (buttonImageWidth + buttonJianJu) * CGFloat(lieIndex)
            let buttonY = buttonJianJu + (buttonHeight + buttonJianJu) * CGFloat(hangIndex) + UIScreen.main.bounds.height
            
            let button = HMSinaComposeButton(frame: CGRect(x: buttonX, y: buttonY, width: buttonImageWidth, height: buttonHeight))
//            button.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
            button.setImage(UIImage(named : "tabbar_compose_" + String(i)), for: .normal)
            switch i {
            case 0:
                button.setTitle("文字", for: .normal)
            case 1:
                button.setTitle("照片/视频", for: .normal)
            case 2:
                button.setTitle("头条文章", for: .normal)
            case 3:
                button.setTitle("签到", for: .normal)
            case 4:
                button.setTitle("直播", for: .normal)
            case 5:
                button.setTitle("更多", for: .normal)
            default:
                return
            }
//            button.setTitle("@好友", for: .normal)
            button.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .normal)
            self.addSubview(button)
            
            buttonArray.append(button)
            
            //监听按钮的点击事件
            button.addTarget(self, action: #selector(clickButton(button:)), for: .touchUpInside)
        }
    }
    
    
    @objc private func clickButton(button : HMSinaComposeButton) {
        
        //被点击的按钮放大，其他的按钮缩小
        UIView.animate(withDuration: 0.5, animations: {
            
            for composeButton in self.buttonArray {
                
                if button == composeButton{
                    
                    //被点击的按钮方放大
                    composeButton.transform = CGAffineTransform.init(scaleX: 1.8, y: 1.8)
                    
                }else{
                    
                    //其他按钮缩小
                    composeButton.transform = CGAffineTransform.init(scaleX: 0.3, y: 0.3)

                }
                //修改alpha
                button.alpha = 0.2
            }
            
        }, completion: { (Bool) in
            
            //跳转对应的控制器
            //通过网络数据已经解析了一个控制名称的字符串
            let bundleName = Bundle.main.infoDictionary?["CFBundleName"] as! String
            
            let className = bundleName + "." + "HMSinaComposeViewController"
            
            //1、获取类型
            guard let classType = NSClassFromString(className) as? UIViewController.Type else{
            
                return
            
            }
            //2、根据类型实例化控制器对象
            let viewController = classType.init()
            //实例化导航视图控制器
            let navigationController = UINavigationController(rootViewController: viewController)
            //3、跳转
            
            UIApplication.shared.keyWindow?.rootViewController?.present(navigationController, animated: true, completion: {
                
                //但是跳转完控制器之后希望立即执行一些操作的时候
                self.removeFromSuperview()
                
            })
            
        })
        
    }
    
    private lazy var backView : UIImageView = UIImageView(image: UIImage.jieQuScreen().applyLightEffect())
    
    private lazy var backGroundView : UIImageView = UIImageView(image: #imageLiteral(resourceName: "compose_slogan"))

    
}
